package com.example.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.entry.User;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class IndexController {

	@Value("${server.port}")
	private String serverPort;

	@GetMapping("/hello")
	public String hello() {
		return "Ribbon query。server port is：" + serverPort;
	}

	@PostMapping("upload")
	public String upload(@RequestParam("file") MultipartFile file) throws IOException {
		Files.copy(file.getInputStream(), Paths.get("F:\\" + file.getOriginalFilename()));
		return "上传成功";

	}

	@GetMapping("/getUser/{id}")
	public User getUser(@PathVariable("id") String id) {
		return new User();
	}

	@GetMapping("/getList")
	public List<User> getUser(User user) {
		log.info("query Eureka-client getList...");
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < 5; i++) {
			list.add(new User(i, "许芬勇" + i, 28 + i, 1));
		}
		return list;
	}

	@PostMapping("addUser")
	public User addUser(@RequestBody User user) {
		return user;
	}

}
